package pl.gladiator.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import pl.gladiator.dao.NewsyEntityManagerImp;
import pl.gladiator.model.Newsy;

@Controller
@RequestMapping("/newsy")
public class NewsyController {
	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@RequestMapping(method=RequestMethod.POST)
    public ModelAndView addNews(Newsy news, BindingResult errors, HttpServletRequest request, HttpServletResponse response){
		if(log.isDebugEnabled())log.debug(news);
		
		NewsyEntityManagerImp nM = new NewsyEntityManagerImp();
		if(news.getNewsPk() == -1){
			 nM.save(news); 
		}else{
			nM.update(news);
		}
        
        List<Newsy> newsList = nM.getAllNews();
        ModelMap map = new ModelMap();
        map.put("newsList", newsList);
        return new ModelAndView("newses", map);
    }
	
	@RequestMapping(value="/newNews", method=RequestMethod.GET)
    public ModelAndView newNews(){
        Newsy news = new Newsy();
        ModelMap map = new ModelMap();
        map.put("news", news);
        return new ModelAndView("addNews", map);
    }
	
	@RequestMapping(method=RequestMethod.GET)
    public String displayAll(HttpServletRequest request){
		NewsyEntityManagerImp nM = new NewsyEntityManagerImp();
        List<Newsy> newsList = nM.getAllNews();
        ModelMap map = new ModelMap();
        map.put("newsList", newsList);
        request.setAttribute("newsList", newsList);
		return "newses";
	}
	
	@RequestMapping(value="/edit/{newsPk}")
    public ModelAndView update(@PathVariable String newsPk){
        ModelMap map = new ModelMap();
        NewsyEntityManagerImp nM = new NewsyEntityManagerImp();
        Newsy news = nM.findNewsByPk(Integer.parseInt(newsPk));
        map.put("news", news);
        return new ModelAndView("addNews", map);
    }
	
	@RequestMapping(value="/delete/{newsPk}")
    public String delete(@PathVariable String newsPk, HttpServletRequest request){
        NewsyEntityManagerImp nM = new NewsyEntityManagerImp();
        nM.delete(Integer.parseInt(newsPk));
        
        List<Newsy> newsList = nM.getAllNews();
        ModelMap map = new ModelMap();
        map.put("newsList", newsList);
        request.setAttribute("newsList", newsList);
        
		return "newses";
    }
	
	@RequestMapping(value="/showNews")
    public String showNews(HttpServletRequest request){
        NewsyEntityManagerImp nM = new NewsyEntityManagerImp();
        
        List<Newsy> newsList = nM.getAllNews();
        ModelMap map = new ModelMap();
        map.put("newsList", newsList);
        request.setAttribute("newsList", newsList);
        
		return "showNews";
    }
	
	
}
